<?php

declare(strict_types=1);

namespace app\index\controller;

use think\facade\View;
use app\BaseController;

class Ai
{
  public function index()
  {
    // 准备请求参数
    $headers = [
      'Authorization: Bearer sk-mrjrxljcuvisbqkwscdfmaqbhahphdayuwensubmmlpqmjem',
      'Content-Type: application/json'
    ];

    $postData = [
      "model" => "deepseek-ai/DeepSeek-R1-Distill-Llama-8B",
      "messages" => [
        [
          "role"    => "user",
          "content" => "你是？"
        ]
      ]
    ];
    // 发送请求 自定义post方法
    $response = post(
      "https://api.siliconflow.cn/v1/chat/completions", // URL
      json_encode($postData),                          // POST数据（已JSON编码）
      [],                                              // URL参数（空数组）
      $headers                                         // 请求头
    );
    halt ($response['choices'][0]['message']['content'] ?? '获取回复内容失败');
    return View::fetch();
  }
}
